package com.example.nettyim.server;

import com.example.nettyim.handler.*;
import com.example.nettyim.protocol.ProcotolFrameDecoder;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * author :sherlock.lin
 * date : Created in 2023/2/13
 * description :
 */
@Slf4j
public class ChatServer {
    public static void main(String[] args) {
        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup worker = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap()
                    .channel(NioServerSocketChannel.class)
                    .group(boss, worker)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new ProcotolFrameDecoder());
                            ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                            ch.pipeline().addLast(new IdleStateHandler(5, 0, 0));
                            ch.pipeline().addLast(new ChannelDuplexHandler(){
                                @Override
                                public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                                    IdleStateEvent event = (IdleStateEvent)evt;
                                    //触发了读空闲事件
                                    if (event.state() == IdleState.READER_IDLE) {
                                        log.debug("已经5s没有读到数据了");
                                        ctx.channel().close();
                                    }
                                }
                            });
                            ch.pipeline().addLast(new LoginRequestMessageHandler());
                            ch.pipeline().addLast(new ChatRequestMessageHandler());
                            ch.pipeline().addLast(new GroupCreateRequestMessageHandler());
                            ch.pipeline().addLast(new GroupMembersRequestMessageHandler());
                            ch.pipeline().addLast(new GroupQuitRequestMessageHandler());
                            ch.pipeline().addLast(new GroupChatRequestMessageHandler());
                            ch.pipeline().addLast(new QuitHandler());

                        }
                    })
        }
    }
}
